<?php
/**
 * Description: 微信web端扫码支付回调url
 * User: devforma
 * Date: 15/6/3
 * Time: 19:50
 */

require PUBLIC_DIR . '/payment/includes/WeixinPay.class.php';
require PUBLIC_DIR . '/payment/includes/OrderProcess.class.php';

class WeixinWebQrNotifyUrl implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        $payment = new WeixinPay();

        $xml = file_get_contents('php://input');

        $para = $payment->xml2array($xml);

        if (!empty($para) && $payment->checkSign($para) &&
            isset($para['result_code']) && $para['result_code'] == 'SUCCESS') {

            $orderProcess = new OrderProcess($para['out_trade_no'], $para['transaction_id'], '', 'weixin');
            if ($orderProcess->isExists() && !$orderProcess->isPurchased()) {
                $orderProcess->finish();
                return $payment->array2xml(array(
                    'return_code' => 'SUCCESS',
                    'return_msg' => 'OK'
                ));
            }
        }

        return $payment->array2xml(array(
            'return_code' => 'FAIL',
            'return_msg' => 'OK'
        ));
    }
}